# coding=utf-8
'''
Created on 2012-8-26

@author: quanwei
'''
from core import HttpTask
from bs4 import * 
from dzdp import BaseAnalyzer
#列表页
class ItemAnalyzer(BaseAnalyzer.BaseAnalyzer):
    #解析网页内容
    def parseHtml(self,html,task):
        newTasks = []
        soup = BeautifulSoup(html,from_encoding="utf-8")
        root = soup.find("div",{ "id" : "searchList" })
        items = root.dl.findAll("dd")
        for item in items:
            if item.get('class') != None:
                continue
            tNode = item.find("a",{"class":"BL"})
            pNode = item.find("strong",{"class":"average"})
            aNode = item.find("li",{"class":"address"})
            tagNodes = item.find("li",{"class":"tags"}).findAll("a")
            
            shop = {"category":"","name":"","url":"","price":0,"addr":"","tags":[]}
            shop['category'] = task.data.get("category")
            shop['city'] = task.data.get("city")
            shop['name'] = tNode.string
            shop['url'] = tNode['href']
            if len(aNode.contents) >= 2:
                shop['addr'] = aNode.get_text().strip()
            if len(pNode.contents) >= 2:
                shop['price'] = pNode.contents[1].strip()
            for tagNode in tagNodes:
                shop['tags'].append(tagNode.string)
            if not self.existsShop(shop):
                self.saveShop(shop)
        
        #任务结束  
        self.complateTask(task)
        
        nextNode = soup.find("a",{"class":"NextPage"})
        if nextNode != None:
            newTask = HttpTask.HttpTask()
            newTask.data['category'] = task.data["category"]
            newTask.data['city'] = task.data["city"]
            newTask.url = "http://www.dianping.com" + nextNode['href']
            newTask.analyzer = ItemAnalyzer()
            if not self.existsTask(newTask):
                self.saveTask(newTask)
                newTasks.append(newTask)
        return newTasks
          
 
        